import 'package:rxdart/rxdart.dart';

import 'package:zz_blog/model/bloc/base_bloc.dart';
import 'package:zz_blog/model/entity/hitokoto.dart';
import 'package:zz_blog/service/common_service.dart';

///
/// 一言bloc
///
/// @author zzzz1997
/// @created_time 20190905
///
class HitokotoBloc extends BaseBloc {
  // 类标签
  static String tag = 'HitokotoBloc';

  // 一言
  Hitokoto _hitokoto = Hitokoto(0, '', '', '', '', '');

  // 获取一言
  Hitokoto get hitokoto => _hitokoto;

  // 一言数据流
  var _hitokotoSubject = BehaviorSubject<Hitokoto>();

  // 获取一言数据流
  Stream<Hitokoto> get hitokotoStream => _hitokotoSubject.stream;

  ///
  /// 初始化
  ///
  init() async {
    try {
      _hitokoto = await CommonService.getHitokoto();
      _hitokotoSubject.add(hitokoto);
    } catch (e) {
      throw e;
    }
  }

  ///
  /// 释放资源
  ///
  dispose() {
    _hitokotoSubject.close();
  }
}
